

'use strict';
const express=require('express');
const path=require('path');
const xtpl=require('xtpl');
const moment=require('moment');
const db=require(path.join(__dirname,"../configs/database.js"));
const base=require(path.join(__dirname,"./baseController.js"));
module.exports.noticeAdd=function(req,res){

   // 老师给班级的学生发通知  先查询老师 再查询班级 
   
   db.table('teach').select().then(function(data){
        db.table('class').select().then(function(cData){       
    xtpl.renderFile(

    path.join(__dirname,"../views/admin/notice_add.html"),
    {
     
       array:data,
       crr:cData

    },
    function(err,content){

        res.setHeader("Content-Type","text/html;charset=utf-8");
        res.end(content);

    }
 	);
        });
  
   });
   
};

module.exports.noticeAddData=function(req,res){


  let where=`notice_titel='${req.body.notice_titel}'`;


  db.table("notice").where(where).find().then(function(data){
          
          if(JSON.stringify(data)!="{}"){

              base.shoWMsg(res,"通知已经存在","/notice/notice_list");
              return false;
          }else{
              let data={

                  "notice_titel":req.body.notice_titel,
                  "notice_content":req.body.notice_content,
                  "notice_time":(Date.parse(moment(req.body.notice_time))/1000),
                  "teach_id":req.body.teach_id,
                  "class_id":req.body.class_id
              };
              db.table('notice').add(data).then(function(affectRows){
                    
                     res.redirect('/notice/notice_list');
                     return false;

              }).catch(function(err){

              	if(err){
              		return console.log(err);
              	}
              });
          }
  });


};


module.exports.noticeList=function(req,res){

 const page=parseInt(req.query.page||'1');
 const limit=parseInt(req.query.limit||'12');

 const start=(page-1)*limit;

 db.table('notice').count("*").then(function(count){

    const totalPage=count%limit==0?count/limit:parseInt(count/limit)+1;
    const totalPageArray=[];
    for(var i=1;i<=totalPage;i++){

    	totalPageArray.push(i);
    };
    
    db.table("notice").join("pre_teach on pre_notice.teach_id=pre_teach.teach_id").join("pre_class on pre_notice.class_id=pre_class.class_id").order("notice_id DESC").limit(start,limit).select().then(function(data){

   xtpl.renderFile(
         path.join(__dirname,"../views/admin/notice_list.html"),
         {

           
        array:data,
    	totalPage:totalPage,
        totalPageArray:totalPageArray,
        page:page


         },
         function(err,content){

              res.setHeader("Content-Type","text/html;charset=utf-8");
              res.end(content);
         }
   	);
        
    });

 });

};

module.exports.noticeEdite=function(req,res){


 let notice_id=req.params.notice_id;
 let where =`notice_id=${notice_id}`;

 //显示旧信息 老师的显示 班级的显示 通知的要显示
 

 db.table('notice').where(where).find().then(function(data){

   db.table('teach').select().then(function(tData){
            
            db.table('class').select().then(function(cData){
                    
               data.notice_time=moment(data.notice_time*1000).format("YYYY-MM-DD");
               xtpl.renderFile(
                  path.join(__dirname,"../views/admin/notice_edite.html"),
                  {

                    noticeInfo:data,
                    arr:tData,
                    crr:cData

                  },
                  function(err,content){

                     res.setHeader("Content-Type","text/html;charset=utf-8");
                     res.end(content);
                  }
               	);
            });
   });


 });





};

module.exports.noticeEditeData=function(req,res){

  let notice_id=req.params.notice_id;
  let where=`notice_id=${notice_id}`;

  db.table('notice').where(where).find().then(function(data){
 
      if(JSON.stringify(data)=="{}"){
           
           base.showMsg(res,'通知不存在','/notice/notice_list');
           return false;

      }else{

          let check=`notice_titel='${req.body.notice_titel}' AND notice_id!=${notice_id}`;

          db.table('notice').where(check).find().then(function(data){
           
                if(JSON.stringify(data)!="{}"){
                	base.showMsg(res,'作业已经存在');
                     return false;
                }else{

                  let data={

                  "notice_titel":req.body.notice_titel,
                  "notice_content":req.body.notice_content,
                  "notice_time":(Date.parse(moment(req.body.notice_time))/1000),
                  "teach_id":req.body.teach_id,
                  "class_id":req.body.class_id
              };

              db.table('notice').where(`notice_id=${notice_id}`).update(data).then(function(affectRows){

                 res.redirect('/notice/notice_list');
                 return false;

              }).catch(function(err,content){
                   
                    if(err){

                    	base.shoeMsg(res,'更新失败','/notice/notice_list');
                    	return false;
                    }
              });

                }

          });
      }

  });
};

module.exports.noticeDelete=function(req,res){


  let notice_id=req.params.notice_id||0;

  let where = `notice_id=${notice_id}`;

  db.table('notice').where(where).delete().then(function(noticeRows){

      base.showMsg(res,'删除成功','/notice/notice_list');           
      return false;
  }).catch(function(err){

      base.showMsg(res,'删除失败','/notice/notice_list');
      return false;

  });
};








